#!/bin/bash
SOURCEDIR=$1
DESTIDIR=$2

find "$SOURCEDIR" -name "*" -type f -print |while read Filename ;  
do 
	#for flash files
	file "$Filename" | grep "Macromedia Flash Video"
	RES=$? ; 
	if [ $RES -eq 0 ] ; 
	then 
		fuser "$Filename" ;
	        RESa=$? ;
	        if [ $RESa -eq 1 ] ;
	        then
        	        echo  Converting == "$Filename" ;
			ffmpeg2theora "$Filename" -o "$Filename".ogg
			rm "$Filename"

	        fi ;

	fi ;
	
	##for mp4 files
	file "$Filename" | grep "MPEG v4 system"
	RES=$? ; 
	if [ $RES -eq 0 ] ; 
	then 
		fuser "$Filename" ;
	        RESa=$? ;
	        if [ $RESa -eq 1 ] ;
	        then
        	        echo  Converting == "$Filename" ;
			ffmpeg2theora "$Filename" -o "$Filename".ogg
			rm "$Filename"

	        fi ;

	fi ;

##for mp3 files
	file "$Filename" | grep "Audio file with ID3 version 2.3, MP3 encoding"
	RES=$? ; 
	if [ $RES -eq 0 ] ; 
	then 
		fuser "$Filename" ;
	        RESa=$? ;
	        if [ $RESa -eq 1 ] ;
	        then
        	        echo  Converting == "$Filename" ;
			ffmpeg2theora "$Filename" -o "$Filename".ogg
			rm "$Filename"

	        fi ;

	fi ;

##for rm files
	file "$Filename" | grep "RealMedia file" 
## size increases on conversion to .ogg by almost 100%
	RES=$? ; 
	if [ $RES -eq 0 ] ; 
	then 
		fuser "$Filename" ;
	        RESa=$? ;
	        if [ $RESa -eq 1 ] ;
	        then
        	        echo  Converting == "$Filename" ;
			ffmpeg2theora "$Filename" -o "$Filename".ogg
			rm "$Filename"

	        fi ;

	fi ;

##for mkv files
	file "$Filename" | grep "Matroska data"
	RES=$? ; 
	if [ $RES -eq 0 ] ; 
	then 
		fuser "$Filename" ;
	        RESa=$? ;
	        if [ $RESa -eq 1 ] ;
	        then
        	        echo  Converting == "$Filename" ;
			ffmpeg2theora "$Filename" -o "$Filename".ogg
			rm "$Filename"

	        fi ;

	fi ;


done
exit 0
